/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.common.utils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Peter on 2016/7/15.
 */
public class MathUtils {

    /**
     * Convert the bigdecimal number to double array.
     *
     * @param data source number.
     * @return double array.
     */
    public static double[] convertBigDecimalToDoubleArray(BigDecimal[] data) {
        return MathUtils.convertBigDecimalToDoubleArray(data, 0);
    }

    /**
     * Convert the bigdecimal number to double array.
     *
     * @param data     source number.
     * @param digitNum the decimal place.
     * @return double array.
     */
    public static double[] convertBigDecimalToDoubleArray(BigDecimal[] data, int digitNum) {
        double[] result = new double[data.length];

        for (int i = 0; i < data.length; i++) {
            data[i].setScale(digitNum, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        return result;
    }

    /**
     * Convert the double number to bigdecimal array.
     *
     * @param data source number.
     * @return bigdecimal array.
     */
    public static BigDecimal[] convertDoubleToBigDecimalArray(double[] data) {
        BigDecimal[] result = new BigDecimal[data.length];
        for (int i = 0; i < data.length; i++) {
            result[i] = new BigDecimal(data[i]);
        }
        return result;
    }

    /**
     * Convert the double number to bigdecimal array.
     *
     * @param data     source number.
     * @param digitNum the decimal place.
     * @return bigdecimal array.
     */
    public static BigDecimal[] convertDoubleToBigDecimalArray(double[] data, int digitNum) {
        BigDecimal[] result = new BigDecimal[data.length];
        for (int i = 0; i < data.length; i++) {
            result[i] = new BigDecimal(data[i]).setScale(digitNum, BigDecimal.ROUND_HALF_UP);
        }
        return result;
    }

    /**
     * Format the double number with decimal place.
     *
     * @param dataValue source value.
     * @param digitNum  the decimal place.
     * @return double value.
     */
    public static double formatDouble(double dataValue, int digitNum) {
        if (digitNum >= 0) {
            return new BigDecimal(dataValue).setScale(digitNum, BigDecimal.ROUND_HALF_UP).doubleValue();
        } else {
            return dataValue;
        }
    }

    /**
     * Get the max one from source data.
     *
     * @param data source value.
     * @return double value.
     */
    public static double findMax(double[] data) {
        double max = data[0];
        for (int i = 1; i < data.length; i++) {
            if (data[i] > max) {
                max = data[i];
            }
        }
        return max;
    }

    /**
     * Get the min one from source data.
     *
     * @param data source value.
     * @return double value.
     */
    public static double findMin(double[] data) {
        double min = data[0];
        for (int i = 1; i < data.length; i++) {
            if (data[i] < min) {
                min = data[i];
            }
        }
        return min;
    }

    /**
     * Get the max one from source data list.
     *
     * @param data source value.
     * @return double value.
     */
    public static double findMax(List<Double> data) {
        double max = data.get(0).doubleValue();
        for (int i = 1; i < data.size(); i++) {
            if (data.get(i).doubleValue() > max) {
                max = data.get(i).doubleValue();
            }
        }
        return max;
    }

    /**
     * Get the min one from source data list.
     *
     * @param data source value.
     * @return double value.
     */
    public static double findMin(List<Double> data) {
        double min = data.get(0).doubleValue();
        for (int i = 1; i < data.size(); i++) {
            if (data.get(i).doubleValue() < min) {
                min = data.get(i).doubleValue();
            }
        }
        return min;
    }

    /**
     * Get the max one from source data array.
     *
     * @param data source array.
     * @return double value.
     */
    public static double findMax(double[][] data) {
        double max = data[0][0];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (max < data[i][j]) {
                    max = data[i][j];
                }
            }
        }
        return max;
    }

    /**
     * Get the min one from source data array.
     *
     * @param data source array.
     * @return double value.
     */
    public static double findMin(double[][] data) {
        double min = data[0][0];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (min > data[i][j]) {
                    min = data[i][j];
                }
            }
        }
        return min;
    }
}
